package weaver.formmode.customjavacode.modeexpand.huangwm.SW003;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;

import com.alibaba.fastjson.JSONObject;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.monitor.cache.CacheFactory;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @author Huangwm
 * @description
 */
public class ResetDBFieldsByModeCard extends AbstractModeExpandJavaCodeNew {
    private Log log = LogFactory.getLog("custom_huangwm");

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        log.info("进入模块扩展动作>ResetDBFieldsByModeCard");
        List<String> list = new ArrayList<String>();
        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    String sql = "select * from uf_xgzd_hwm_dt1 where mainid=" + billid;
                    log.info(sql);
                    try {
                        RecordSet rs = new RecordSet();
                        rs.execute(sql);
                        if (rs.getDBType().equals("oracle")) {
                            log.info("数据库为oracle");
                            list.add("数据库为oracle;");
                            RecordSet rs2 = new RecordSet();
                            while (rs.next()) {
                                String zdid = rs.getStringNoTrim("zdid");
                                String zdm = rs.getStringNoTrim("zdm");
                                String dqzd = rs.getStringNoTrim("dqzd");
                                String szb = rs.getStringNoTrim("szb");
                                String xghzd = rs.getStringNoTrim("xghzd");
                                boolean r1 = rs2.execute("alter table " + szb + " rename column " + dqzd + " to " + xghzd);
                                boolean r2 = rs2.execute("update workflow_billfield set fieldname='" + xghzd + "' where id=" + zdid);
                                list.add("表[" + szb + "]字段" + zdm + "由[" + dqzd + "]修改为[" + xghzd + "]-->修改结果 r1=" + r1 + ",r2=" + r2);
                            }
                        } else {
                            log.info("数据库为mssql或sqlserver");
                            list.add("数据库为mssql或sqlserver;");
                            RecordSet rs2 = new RecordSet();
                            while (rs.next()) {
                                String zdid = rs.getStringNoTrim("zdid");
                                String zdm = rs.getStringNoTrim("zdm");
                                String dqzd = rs.getStringNoTrim("dqzd");
                                String szb = rs.getStringNoTrim("szb");
                                String xghzd = rs.getStringNoTrim("xghzd");
                                boolean r1 = rs2.execute("sp_rename '" + szb + "." + dqzd + "','" + xghzd + "'");
                                boolean r2 = rs2.execute("update workflow_billfield set fieldname='" + xghzd + "' where id=" + zdid);
                                list.add("表[" + szb + "]字段" + zdm + "由[" + dqzd + "]修改为[" + xghzd + "]-->修改结果 r1=" + r1 + ",r2=" + r2);
                            }
                        }
                        rs.execute("update uf_xgzd_hwm set xgjg='" + JSONObject.toJSONString(list) + "' where id=" + billid);
                        CacheFactory.getInstance().clear();
                        CacheFactory.getInstance().reset();
                    } catch (Exception e) {
                        log.info("1 " + e.getMessage());
                        e.printStackTrace();
                        result.put("errmsg", "1 保存動作擴展接口異常 \n" + e.getMessage());
                        result.put("flag", "false");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("2 " + e.getMessage());
            result.put("errmsg", "2 保存動作擴展接口異常 \n" + e.getMessage());
            result.put("flag", "false");
        }
        log.info(JSONObject.toJSONString(list));
        return result;
    }

}
